package com.nanbei.eduService.controller.front;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanbei.commonUntil.JwtUtils;
import com.nanbei.commonUntil.R;
import com.nanbei.commonUntil.vo.orderVo.CourseWebVoOrder;
import com.nanbei.eduService.client.OrderClient;
import com.nanbei.eduService.entity.EduChapter;
import com.nanbei.eduService.entity.EduCourse;
import com.nanbei.eduService.entity.frontVo.CourseFrontQueryVo;
import com.nanbei.eduService.entity.frontVo.CourseWebVo;
import com.nanbei.eduService.entity.vo.ChapterVo;
import com.nanbei.eduService.service.EduChapterService;
import com.nanbei.eduService.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(description = "前端课程管理")
@RestController
@RequestMapping("/eduService/frontCourse")
@CrossOrigin  //解决跨域问题
public class CourseFrontController {


    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private OrderClient orderClient;

    //分页条件查询课程的方法
    @ApiOperation(value = "分页查询讲师）")
    @PostMapping("/pageCourseFront/{current}/{limit}")
    public R pageCourseFront(@PathVariable long current,
                             @PathVariable long limit,
                             @RequestBody CourseFrontQueryVo courseFrontQueryVo){

        Page<EduCourse> eduCoursePage = new Page<>(current,limit);

        IPage<EduCourse> eduCourseList = eduCourseService.pageCourseFront(eduCoursePage,courseFrontQueryVo);

        return R.ok().data("total",eduCourseList.getTotal()).data("records",eduCourseList.getRecords());

    }


    /**
     * 根据课程ID查询课程详细信息
     * @param courseId
     * @return
     */
    @GetMapping("/findCourseInfoById/{courseId}")
    public R findCourseInfoById(@PathVariable String courseId, HttpServletRequest request){
        //根据课程ID查询课程章节和小节信息
        List<ChapterVo> chapterVideoById = eduChapterService.findChapterVideoById(courseId);

        //根据课程ID查询课程详细信息
        CourseWebVo courseWebVo = eduCourseService.findCourseInfo(courseId);

        //根据课程ID和用户ID判断该用户是否购买该课程
        boolean notBuy = orderClient.isNotBuy(courseId, JwtUtils.getMemberIdByJwtToken(request));
        System.out.println(notBuy);

        return R.ok().data("chapterVideoById",chapterVideoById).data("courseWebVo",courseWebVo).data("isbuy",notBuy);
    }

    /**
     * 订单根据课程Id 获取课程详细信息
     * @param courseId
     * @return
     */
    @GetMapping("orderFindCourseInfo/{courseId}")
    public CourseWebVoOrder orderFindCourseInfo(@PathVariable String courseId){
        //根据课程ID查询课程详细信息
        CourseWebVo courseWebVo = eduCourseService.findCourseInfo(courseId);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }

}
